package com.rpc.provider.handler;

import com.rpc.common.entity.request.RPCDTO;
import com.rpc.provider.invoker.RPCInvoker;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * rpc服务处理器
 */
public class RPCProcessor implements Runnable {
    private Socket socket;

    public RPCProcessor(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream()); //获得输入流
            RPCDTO rpcdto = (RPCDTO) objectInputStream.readObject();
            Object resObj = RPCInvoker.invoke(rpcdto);  //调用
            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            outputStream.writeObject(resObj);   //写会响应
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }
}
